package com.hex.liyue.storemodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.config.Constant;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.utils.DateUtils;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.storemodule.presenter.IPaymentOrdersView;
import com.hex.liyue.storemodule.presenter.PaymentOrdersPresenter;
import com.hex.liyue.usermodule.activity.OrderDetailsActivity;
import com.hex.liyue.usermodule.bean.OrderDetailsResultBean;
import com.tencent.mm.sdk.openapi.IWXAPI;

import butterknife.Bind;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * 支付订单界面
 */
public class PaymentOrdersActivity extends BaseActivity implements IPaymentOrdersView {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_pay_order_price)
    TextView tvPayOrderPrice;
    @Bind(R.id.tv_balance)
    TextView tvBalance;
    @Bind(R.id.iv_balance)
    ImageView ivBalance;
    @Bind(R.id.iv_wechat)
    ImageView ivWechat;
    @Bind(R.id.iv_alipay)
    ImageView ivAlipay;

    private String orderId;
    private int type = 0;
    private boolean canPay = false;
    boolean isOtherRun = false;
    private PaymentOrdersPresenter paymentOrdersPresenter;
    private String name;
    private String desc;
    private IWXAPI msgApi;

    @Override
    protected void bindPresenter() {
        paymentOrdersPresenter = new PaymentOrdersPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        EventBus.getDefault().register(this);
        isOtherRun = getIntent().getBooleanExtra("isOtherRun", false);
        orderId = extras.getString("order_id");
        name = extras.getString("name]");
        desc = extras.getString("desc");
        paymentOrdersPresenter.getOrderDetails(orderId);

    }

    /**
     * 接收微信支付回调页面的发送的状态消息
     *
     * @param event
     */
    public void onEvent(String event) {
        if (event.equals(Constant.WX_CANCEL)) {
            showT("支付取消");
        } else if (event.equals(Constant.WX_SUCCESS)) {
            showT("支付成功");
            startActivityOrderDetails();
        } else if (event.equals(Constant.WX_FAILURE)) {
            showT("支付失败");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_payment_order;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarTitle.setText(getApplication().getResources().getString(R.string.pay_orders_title));
    }


    @OnClick({R.id.iv_toobar_back, R.id.bt_store_pay, R.id.rl_balance_box, R.id.rl_wechat_box, R.id.rl_alipay_box})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
            case R.id.rl_balance_box:
                //余额支付
                if (canPay) {
                    type = 1;
                    ivBalance.setImageResource(R.drawable.pay_checkbox_press);
                    ivWechat.setImageResource(R.drawable.pay_radio_normal);
                    ivAlipay.setImageResource(R.drawable.pay_radio_normal);
                } else {
                    type = 0;

                }
                break;
            case R.id.rl_wechat_box:
                //微信支付
                type = 2;
                ivWechat.setImageResource(R.drawable.pay_radio_press);
                ivBalance.setImageResource(R.drawable.pay_checkbox_normal);
                ivAlipay.setImageResource(R.drawable.pay_radio_normal);
                break;
            case R.id.rl_alipay_box:
                //支付宝支付
                type = 3;
                ivAlipay.setImageResource(R.drawable.pay_radio_press);
                ivBalance.setImageResource(R.drawable.pay_checkbox_normal);
                ivWechat.setImageResource(R.drawable.pay_radio_normal);
                break;
            case R.id.bt_store_pay://确认支付

                if (type == 1) {
                    //余额支付
                    paymentOrdersPresenter.showinputpassworddialog(orderId, type, PaymentOrdersActivity.this, name, desc);
                } else if (type == 3) {
                    //支付宝支付
                    paymentOrdersPresenter.orderPay(orderId, type, PaymentOrdersActivity.this, null, name, desc);
                } else if (type == 2) {
                    //微信支付
                    // showT("暂不支持微信支付，请使用其他付款方式哦~");
                    paymentOrdersPresenter.orderPay(orderId, type, PaymentOrdersActivity.this, null, name, desc);
                } else {
                    //余额不足
                    showT("余额不足");
                }
                break;
        }
    }


    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

    @Override
    public void startActivityOrderDetails() {
        //进入订单详情
        if (isOtherRun) {
            Intent intent = new Intent(this, OrderDetailsActivity.class);
            intent.putExtra("orderId", orderId);
            startActivity(intent);
            setResult(RESULT_OK);
            finish();
        } else {
            setResult(RESULT_OK);
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_CANCELED);
        finish();
    }

    @Override
    public void setInfo(OrderDetailsResultBean.DataBean dataBean) {
        tvTime.setText(DateUtils.getFormatDate(dataBean.create_time * 1000, "yyyy-MM-dd HH:mm:ss"));
        tvPayOrderPrice.setText(dataBean.pay_money + "元");
        tvBalance.setText("￥" + UserManager.getBalance());
        if (UserManager.getBalance() > dataBean.pay_money) {
            //余额可以支付
            ivBalance.setImageResource(R.drawable.pay_checkbox_press);
            type = 1;
            canPay = true;
        } else {
            //余额不足
            ivAlipay.setImageResource(R.drawable.pay_radio_press);
            tvBalance.setText("￥" + UserManager.getBalance() + " (余额不足)");
            ivBalance.setVisibility(View.GONE);
            canPay = false;
            type = 3;
        }
    }

}
